<template>
  <div>
    <el-dialog
      :title="status.isAdd ? '菜单添加' : '菜单编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="菜单名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" label-width="120px">
          <el-select
            v-model="form.pid"
            placeholder="请选择活动区域"
            @change="changePid"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" label-width="120px">
          <el-radio v-model="form.type" :label="1" disabled>目录</el-radio>
          <el-radio v-model="form.type" :label="2" disabled>菜单</el-radio>
        </el-form-item>
        <el-form-item
          label="菜单图标"
          label-width="120px"
          v-if="form.type == 1"
        >
          <el-select v-model="form.icon" placeholder="请选择活动区域">
            <el-option
              v-for="item in icons"
              :key="item"
              :label="item"
              :value="item"
            >
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" label-width="120px" v-else>
          <el-select v-model="form.url" placeholder="请选择活动区域">
            <el-option
              v-for="item in IndexRoutes"
              :key="item.path"
              :label="item.meta.title"
              :value="'/index/' + item.path"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="status.isshow = false">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="updata">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { reqmenuadd, reqmenuinfo, reqmenuedit } from "../../../http/api";
import { IndexRoutes } from "../../../router/index";
import { successAlert, errorAlert } from "../../../utils/alert";

export default {
  components: {},
  props: ["status", "list"],
  data() {
    return {
      form: {
        pid: "",
        title: "",
        icon: "",
        type: "",
        url: "",
        status: "",
      },
      icons: [
        "el-icon-setting",
        "el-icon-user-solid",
        "el-icon-s-goods",
        "el-icon-help",
      ],
      IndexRoutes,
    };
  },
  methods: {
    add() {
      // 发请求
      reqmenuadd(this.form).then((res) => {
        this.checkeddata().then(() => {
          if (res.data.code == 200) {
            // 1.弹成功
            successAlert(res.data.msg);
            // 2.form的数据清空一下
            this.empty();
            // 3.弹框消失
            this.status.isshow = false;
            // 4.列表需要重新渲染
            this.$emit("init");
          }
        });
      });
    },
    //验空
    checkeddata() {
      return new Promise((reslove) => {
        if (this.form.title == "") {
          errorAlert("菜单名称不能为空");
          return;
        }
        if (this.pid == "") {
          errorAlert("上级分类不能为空");
          return;
        }
        reslove();
      });
    },
    //清空
    empty() {
      this.form = {
        pid: "",
        title: "",
        icon: "",
        type: "",
        url: "",
        status: "",
      };
    },
    changePid() {
      // 上级菜单是顶级菜单的话，就是要创建目录；
      // 否则，就是要创建菜单
      if (this.form.pid == 0) {
        this.form.type = 1;
      } else {
        this.form.type = 2;
      }
    },
    //修改
    getOne(id) {
      reqmenuinfo({
        id: id,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          //此处form里面没有id
          this.form.id = id;
        }
      });
    },
    //修改
    updata() {
      reqmenuedit(this.form).then((res) => {
        this.checkeddata().then(() => {
          if (res.data.code == 200) {
            // 1.弹成功
            successAlert(res.data.msg);
            // 2.form的数据清空一下
            this.empty();
            // 3.弹框消失
            this.status.isshow = false;
            // 4.列表需要重新渲染
            this.$emit("init");
          }
        });
      });
    },
    // 9.弹框关闭
    closed() {
      // 如果是添加的取消，什么都不做；如果是编辑的取消，form清空
      if (!this.status.isAdd) {
        this.empty();
      }
    },
  },
  mounted() {},
  computed: {},
  watch: {},
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>
